<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="public/css.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <title>tranthequang.it</title>
    </head>
    <body>
        <h1>Tran The Quang</h1>
        <?php require_once 'model/db.php'; ?>
        <?php
        $query = "SELECT * FROM companies";
        $companies = getPages($query);
        $query = "SELECT * FROM apps";
        $apps = getPages($query);
        ?>
        <form method="POST">
            Min &sopf;&sopf;:<input type="text" name="min_fans" id="min_fans"><br>
            Max &sopf;&sopf;:<input type="text" name="max_fans" id="max_fans"><br>
            Keyword :<input type="text" name="keyword"><br>
            Company :<select name="id_company">
                <option value="0"><-----company-----></option>
                <?php foreach ($companies as $company): ?>
                    <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
                <?php endforeach; ?>
            </select><br>

            App :<select name="id_apps">
                <option value="0"><-----App-----></option>
                <?php foreach ($apps as $app): ?>
                    <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
                <?php endforeach; ?>
            </select><br>

            <input type="submit" name="submit" value="Search">
        </form>

        <?php if (isset($_POST['submit'])): ?>
            <?php
            $min_fans = $_POST['min_fans']; //TODO
            $max_fans = $_POST['max_fans']; //TODO
            $keyword = $_POST['keyword']; //TODO
            $company = $_POST["id_company"];
            $apps = $_POST["id_apps"];
            if (!empty($company))
                $company = "(companies.id=$company) AND";
            else
                $company = "";
            if (!empty($apps))
                $apps = "(apps.id='$apps') AND";
            else
                $apps = "";

            if (empty($max_fans) && !empty($min_fans)) {
                $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps 
                  (pages.likes>= $min_fans) and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,20";
            } else if (!empty($max_fans) && empty($min_fans)) {
                $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  (pages.likes<= $max_fans) and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,20";
            } else if (empty($max_fans) && empty($min_fans)) {
                $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,20";
            } else {
                $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  (pages.likes>= $min_fans) and
                  (pages.likes<= $max_fans) and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,20";
            }
            var_dump($query);
            $pages = getPages($query);
            ?>
            <?php if (!empty($pages)): ?>
                <table id="table1" border="1px">
                    <tr class="khung">
                        <td class="pid">STT </td>
                        <td class="id_page">id_page </td>
                        <td class="name">name </td>
                        <td class="likes">likes </td>
                        <td class="talking">talking</td>
                        <td class="category">category </td>
                        <td class="link">link </td>
                        <td class="website">website </td>
                        <td class="picture">picture </td>
                        <td class="description">description </td>
                    </tr>
                    <?php foreach ($pages as $page): ?>
                        <tr>
                            <td><?php echo $page["pid"] ?></td>
                            <td><?php echo $page["id"] ?></td>
                            <td><?php echo $page["name"] ?></td>
                            <td><?php echo $page["likes"] ?></td>
                            <td><?php echo $page["talking_about_count"] ?></td>
                            <td><?php echo $page["category"] ?></td>
                            <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                            <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                            <td><?php echo substr($page['description'], 0, 200) ?></td>
                        </tr>
                    <?php endforeach; ?>
                </table> 
            <?php endif; ?>
                <script type="text/javascript">
                $(document).ready(function() {
                    $('#min_fans').click(function() {
                        var min_fans = $('#min_fans').val();                    
                        if ($.isNumeric(min_fans)) {
                            alert('is number').value();
                        } else {
                            alert('is not number').value();
                        }
                    });
                });
            </script>
        <?php endif; ?>
    </body>
</html>